using IQIGame.Onigao.Framework;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.Game.Editors
{
    [CustomEditor(typeof(UIMaterialParamter))]
    [CanEditMultipleObjects]
    public class UIMaterialParamterEditor : Editor
    {
        const string keyStr = "key";

        UIMaterialParamter source;
        private void OnEnable()
        {
            source = (UIMaterialParamter)base.target;
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            Undo.RecordObject(target, null);

            GUI.changed = false;
            {
                EditorGUI.BeginChangeCheck();

                EditorGUILayout.Space(10);

                EditorGUILayout.BeginHorizontal();

                EditorGUILayout.LabelField(keyStr, GUILayout.MaxWidth(60));
                source.key = EditorGUILayout.TextField(source.key);
                source.type = (UIMaterialParamter.PropertyType)EditorGUILayout.EnumPopup(source.type, GUILayout.MaxWidth(60));
                switch (source.type)
                {
                    case UIMaterialParamter.PropertyType.Float:
                        source.fValue = EditorGUILayout.FloatField(source.fValue);
                        break;
                    case UIMaterialParamter.PropertyType.Vector:
                        source.vValue = EditorGUILayout.Vector4Field(string.Empty, source.vValue);
                        break;
                    case UIMaterialParamter.PropertyType.Color:
                        source.cValue = EditorGUILayout.ColorField(source.cValue);
                        break;
                }

                EditorGUILayout.EndHorizontal();
            }

            if (GUI.changed)
            {
                EditorUtility.SetDirty(this.source);
                serializedObject.ApplyModifiedProperties();
            }
        }
    }
}
